<?php

namespace app\admin\controller;
use app\Base;
use app\service\admin\UserService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
use think\response\Json;
use think\response\View;

class User extends Base
{

    public array $rule = [
        'name' => 'require|max:25',
        'age' => 'require|number|between:1,120',
        'phone' => 'require|mobile',
    ];

    public array $message = [
        'name.require' => '用户名不能为空',
        'name.max' => '用户名最长25的字符',
        'age.require' => '年龄不能为空',
        'age.number' => '年龄为数字',
        'age.between' => '年龄应再1-120之间',
        'phone.require' => '手机号不能为空',
        'phone.mobile' => '手机号无效',
    ];

    /**
     * 首页
     * @return View
     * @author Du
     * @date 2021/3/31 11:45
     */
    public function show(): View
    {
        return view('admin/user/index');
    }

    /**
     * 获取用户列表
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author Du
     * @date 2021/3/31 11:45
     */
    public function userList()
    {
        $page = $this->request->param('page', 1);
        $pageSize = $this->request->param('limit', 15);
        $so = $this->request->param('so', '');
        $res = UserService::getUserList($page, $pageSize, $so);
        $this->res['count'] = $res['count'];
        $this->res['data'] = $res['data'];
        $this->res['page'] = $page;
        return $this->returnData();
    }

    /**
     * 添加用户
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author Du
     * @date 2021/3/31 18:33
     */
    public function addUser(): Json
    {
        $data = $this->request->param();
        try {
            validate($this->rule, $this->message)->check($data);
        } catch (ValidateException $e) {
            return $this->returnData(1, $e->getError());
        }
        $res = UserService::addData($data);
        if (!$res) {
            return $this->returnData(99, '添加失败');
        } elseif ($res === -1) {
            return $this->returnData(1, '手机号已存在');
        }
        return $this->returnData();
    }

    /**
     * 修改用户数据
     * @return Json
     * @author Du
     * @date 2021/4/1 15:23
     */
    public function updateUser(): Json
    {
        $data = $this->request->param();
        try {
            validate($this->rule, $this->message)->check($data);
        } catch (ValidateException $e) {
            return $this->returnData(1, $e->getError());
        }
        $res = UserService::updateData($data);
        if (!$res){
            return $this->returnData(1, '修改失败');
        }
        return $this->returnData();
    }

    /**
     * 删除用户
     * @return Json
     * @throws DbException
     * @author Du
     * @date 2021/4/1 16:30
     */
    public function deleteUser()
    {
        $data = $this->request->param('data', '');
        $res = UserService::deleteData($data);
        if (!$res){
            return $this->returnData(1, '删除失败，记录不存在');
        }
        return $this->returnData();
    }

}
